<?php

namespace App\Services;

class BaseService
{
    public function getTree(array $data, int $parentId = 0, string $key = 'id'): array
    {
        $tree = [];

        foreach ($data as $row) {
            if ($row['parent_id'] == $parentId) {
                $tmp = $this->getTree($data, $row[$key]);
                if (!empty($tmp)) {
                    $row['children'] = $tmp;
                }
                $tree[] = $row;
            }
        }

        return $tree;
    }

}
